﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft;

public partial class Customer_CustomerEdit : BasePage
{
    #region 属性
    /// <summary>
    /// Guid
    /// </summary>
    private string id
    {
        get
        {
            return Request.QueryString["CustomerId"] == null ? "" : Request.QueryString["CustomerId"].ToString();
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    public string Action
    {
        get
        {
            return Request.QueryString["Action"] == null ? "" : Request.QueryString["Action"].ToString();
        }
    }

    public string pageIndex
    {
        get
        {
            return Request.QueryString["pageIndex"] == null ? "0" : Request.QueryString["pageIndex"].ToString();
        }
    }
    #endregion

    T_Customer model = new T_Customer();
    T_CustomerBLL bll = new T_CustomerBLL();
    T_ClassificationBLL class_bll = new T_ClassificationBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        setUserControlStatus();

        if (!IsPostBack)
        {
            BindDropDownList();

            if (Action == "Modify" || Action == "Detail" || Action == "HDetail")
            {
                getDate(id);
            }

            SetControlStatus();
        }
    }

    private void SetControlStatus()
    {
        bool bIsEnabled = (Action == "HAdd" || Action == "Modify");
        this.ddl_CustomerType.Enabled = bIsEnabled;
        this.txt_CustomerName.Enabled = bIsEnabled;
        this.txt_Country.Enabled = bIsEnabled;
        this.txt_Email.Enabled = bIsEnabled;
        this.txt_Phone.Enabled = bIsEnabled;
        this.txt_CompanyName.Enabled = bIsEnabled;
        this.txt_Skype.Enabled = bIsEnabled;
        this.txt_Whatsapp.Enabled = bIsEnabled;
        this.txt_QQ.Enabled = bIsEnabled;
        this.txt_Wechat.Enabled = bIsEnabled;
        this.txt_Webside.Enabled = bIsEnabled;
        this.txt_Adress.Enabled = bIsEnabled;
        this.txt_ProductDescription.Enabled = bIsEnabled;
        this.txt_CustomerReview.Enabled = bIsEnabled;
        this.txt_TargetMarket.Enabled = bIsEnabled;
        this.txt_Schedule.Enabled = bIsEnabled;
        this.rbl_Gender.Enabled = bIsEnabled;
        this.txt_Wangwang.Enabled = bIsEnabled;

        this.btn_Add.Visible = (Action == "HDetail");
        this.btn_Back.Visible = (Action != "HAdd" && Action != "HDetail");
        this.btn_Modify.Visible = (Action == "Detail");
        this.btn_Save.Visible = (Action == "Modify" || Action == "HAdd");

        if (Action == "HAdd" || Action == "HDetail")
        {
            this.lbl_Title.Text = "客户资料录入";
        }
        else if (Action == "Modify")
        {
            this.lbl_Title.Text = "客户资料编辑";
        }
        else if (Action == "Detail")
        {
            this.lbl_Title.Text = "客户资料查看";
        }
    }
    #endregion

    #region 检验用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission("002002");
        this.btn_Modify.Enabled = base.CheckUserPermissionOperation("002002", "Add");
        this.btn_Save.Enabled = base.CheckUserPermissionOperation("002002", "Add") || base.CheckUserPermissionOperation("002002", "Edit");
    }
    #endregion

    #region 绑定数据

    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="id"></param>
    private void getDate(string id)
    {
        model = bll.GetModel(new Guid(id));
        if (model == null)
        {
            return;
        }
        this.ddl_CustomerType.SelectedValue = model.CustomerTypeId.ToString();
        this.txt_CustomerName.Text = model.CustomerName;
        this.txt_Country.Text = model.Country;
        this.txt_Email.Text = model.Email;
        this.txt_Phone.Text = model.Phone;
        this.txt_CompanyName.Text = model.CompanyName;
        this.txt_Skype.Text = model.Skype;
        this.txt_Whatsapp.Text = model.Whatsapp;
        this.txt_QQ.Text = model.QQ;
        this.txt_Wechat.Text = model.Wechat;
        this.txt_Webside.Text = model.Webside;
        this.txt_Adress.Text = model.Adress;
        this.txt_ProductDescription.Text = model.ProductDescription;
        this.txt_CustomerReview.Text = model.CustomerReview;
        this.txt_TargetMarket.Text = model.TargetMarket;
        this.txt_Schedule.Text = model.Schedule;
        this.rbl_Gender.SelectedValue = model.Gender.ToString();
        this.txt_Wangwang.Text = model.Wangwang;
    }

    private void BindDropDownList()
    {
        //类型
        List<T_Classification> class_list = class_bll.GetModelList("1=1");

        //屏幕
        int screen_id = class_list.Find(o => o.ClassificationCode == "CustomerType").ClassificationId;
        List<T_Classification> screen_list = class_list.FindAll(o => o.ParentId == screen_id);
        this.ddl_CustomerType.DataTextField = "ClassificationName";
        this.ddl_CustomerType.DataValueField = "ClassificationId";
        this.ddl_CustomerType.DataSource = screen_list;
        this.ddl_CustomerType.DataBind();
        this.ddl_CustomerType.Items.Insert(0, new ListItem("----请选择----", "0"));
    }
    #endregion

    #region 提交
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (Action == "Modify")
        {
            model = bll.GetModel(new Guid(id));
        }
        else if (Action == "HAdd")
        {
            model = new T_Customer();
        }
        else
        {
            base.Alert("异常操作！");
            return;
        }
        model.CustomerTypeId = Convert.ToInt32(this.ddl_CustomerType.SelectedValue);
        model.CustomerName = this.txt_CustomerName.Text.Trim();
        model.Country = this.txt_Country.Text.Trim();
        model.Email = this.txt_Email.Text.Trim();
        model.Phone = this.txt_Phone.Text.Trim();
        model.CompanyName = this.txt_CompanyName.Text.Trim();
        model.Skype = this.txt_Skype.Text.Trim();
        model.Whatsapp = this.txt_Whatsapp.Text.Trim();
        model.QQ = this.txt_QQ.Text.Trim();
        model.Wechat = this.txt_Wechat.Text.Trim();
        model.Webside = this.txt_Webside.Text.Trim();
        model.Adress = this.txt_Adress.Text.Trim();
        model.ProductDescription = this.txt_ProductDescription.Text.Trim();
        model.CustomerReview = this.txt_CustomerReview.Text.Trim();
        model.TargetMarket = this.txt_TargetMarket.Text.Trim();
        model.Schedule = this.txt_Schedule.Text.Trim();
        model.Gender = Convert.ToInt32(this.rbl_Gender.SelectedValue);
        model.Wangwang = this.txt_Wangwang.Text.Trim();

        model.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        model.UpdateUserId = base.CurrentSession.UserID;
        model.Valid = 1;

        try
        {
            if (Action == "Modify")
            {
                //修改
                bll.Update(model);

            }
            else if (Action == "HAdd")
            {
                model.CreateTime = model.UpdateTime;
                model.CreateUserId = model.UpdateUserId;
                model.CustomerId = Guid.NewGuid();

                //保存
                bll.Add(model);
            }

            if (Action == "HAdd")
            {
                base.Redirect("操作成功！", "CustomerEdit.aspx?Action=HDetail&CustomerId=" + model.CustomerId.ToString());
            }
            else
            {
                //base.Redirect("操作成功！", "CustomerEdit.aspx?Action=Detail&CustomerId=" + model.CustomerId.ToString());
                base.CloseWindowAndRefresh();
            }

        }
        catch (Exception ex)
        {
            base.AlertError(ex.ToString());
        }
    }
    #endregion

    #region 编辑按钮
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("CustomerEdit.aspx?Action=Modify&CustomerId={0}", id));
    }
    #endregion
}